/*******************************************************************************
 * Open Battery is free software. You can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the 
 * Free Software Foundation; either version 2 of the License, or (at your 
 * option) any later version.
 *
 * Gareth Jones  gljones@ic.ac.uk
 *
 * Some of the code in this project is from the Android Battery Dog. The 
 * license information for Android Battery Dog is copied below.
 *
 *******************************************************************************/

/*******************************************************************************
 * Copyright (c) 2009 Ferenc Hechler - ferenc_hechler@users.sourceforge.net
 * 
 * This file is part of the Android Battery Dog
 *
 * The Android Battery Dog is free software;
 * you can redistribute it and/or modify it under the terms of the GNU
 * General Public License as published by the Free Software Foundation;
 * either version 2 of the License, or (at your option) any later version.
 * 
 * The Android Battery Dog is distributed
 * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with the Android Battery Dog;
 * if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *  
 *******************************************************************************/
package uk.ac.imperial.doc.aesop.openbattery;

import java.io.BufferedReader;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import uk.ac.imperial.doc.aesop.openbattery.R;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.RadioGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;
import android.widget.ToggleButton;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.util.Log;

public class OpenBattery extends Activity {

	public static final String PREFS_NAME = "openbatterypreferences";
	
	private ToggleButton tbStart;
	private Button btSubmit;
	
	private ProgressBar mProgress;
	
	private RadioGroup radioGroupAutoSend;
	private RadioButton radioButtonNever;
	private RadioButton radioButtonCharging;
	private RadioButton radioButtonChargingWifi;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		// Load layout elements		
		
		// find progress bar
		mProgress = (ProgressBar) findViewById(R.id.progressBar);
		
		// find buttons in view
		tbStart = ((ToggleButton) findViewById(R.id.toggleButtonStart));
		btSubmit = ((Button) findViewById(R.id.btSubmit));
		
		radioGroupAutoSend = ((RadioGroup) findViewById(R.id.radioGroupAutoSend));
		radioButtonNever = ((RadioButton) findViewById(R.id.radio_never));
		radioButtonCharging = ((RadioButton) findViewById(R.id.radio_charging));
		radioButtonChargingWifi = ((RadioButton) findViewById(R.id.radio_chargingwifi));
		
		// set actions for buttons
		tbStart.setOnClickListener(StartServiceListener);
		btSubmit.setOnClickListener(SubmitDataListener);
		radioGroupAutoSend.setOnCheckedChangeListener(updateAutoSendPrefs);
		
		// Find user automatic submission preferences
		SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
		SharedPreferences.Editor editor = settings.edit();

		Integer autoSubmit = Integer.parseInt(settings.getString("autoSubmit","2"));
		
		// autoSubmit options
		//Log.d("openbattery","autoSubmit " + autoSubmit);
		
		if (autoSubmit == 1) {
			Log.d("openbattery","loading autoSubmit = 1");
			radioGroupAutoSend.check(radioButtonCharging.getId());
		} 
		
		if (autoSubmit == 2) {
			Log.d("openbattery","loading autoSubmit = 2");
			radioGroupAutoSend.check(radioButtonChargingWifi.getId());
		}
			
		if (  (autoSubmit != 1) &&  (autoSubmit != 2) ) {
			Log.d("openbattery","autoSubmit off");
			radioGroupAutoSend.check(radioButtonNever.getId());
		}
		

		// Make sure deviceid is set

		String deviceid = settings.getString("deviceid","");
		
		if (deviceid.equalsIgnoreCase("")) {
			deviceid = Long.toHexString(Double.doubleToLongBits(Math.random()));

    		editor.putString("deviceid",deviceid);
    		editor.commit();
		}

		// Set ToggleBotton if logging service is running
		
		if (isMyServiceRunning()) {
			
			// Change text to say "stop logging" on the ToggleButton
			tbStart.setText(getApplicationContext().getString(R.string.tbTextOn));
			tbStart.setChecked(true);
			
		} else {
			
			// if logging service is not running, start the logging service
			
			try {
				// Start service
				
				startService(new Intent(OpenBattery.this,OpenBatteryService.class));
		        tbStart.setChecked(true);

			} catch (Exception e) {	

				// TODO
				
			}
			
		}

	}

	
	// Check Internet connection, from http://stackoverflow.com/questions/1560788/
	
	private boolean isNetworkAvailable() {
	    ConnectivityManager connectivityManager 
	          = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
	    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
	    return activeNetworkInfo != null;
	}
	
	// Check if service is running
	// From http://stackoverflow.com/questions/600207/
	
	private boolean isMyServiceRunning() {
	    ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
	    for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
	        if ("uk.ac.imperial.doc.aesop.openbattery.OpenBatteryService".equals(service.service.getClassName())) {
	            return true;
	        }
	    }
	    return false;
	}
	
	OnClickListener StartServiceListener = new OnClickListener() {
		public void onClick(View v) {
			
			// Check if service is already running
			
			if(isMyServiceRunning()) {
				
				try {
					
					// Stop service
					stopService(new Intent(OpenBattery.this,OpenBatteryService.class));
					tbStart.setChecked(false);
					
				} catch (Exception e) {
					
					// TODO
					
				}
			
			} else {
				
				try {
					
					// Start service
					
					startService(new Intent(OpenBattery.this,OpenBatteryService.class));
		            
		            //Toast.makeText(getApplicationContext(), "Open Battery logging started", Toast.LENGTH_SHORT).show();
			        tbStart.setChecked(true);

				} catch (Exception e) {	
				
					//Toast.makeText(OpenBattery.this,"Start Service failed: " + e.getMessage(),Toast.LENGTH_SHORT).show();

				}
				
			}
		
		}
	};


	OnClickListener SubmitDataListener = new OnClickListener() {

		public void onClick(View v) {

			btSubmit = ((Button) findViewById(R.id.btSubmit));
			
			btSubmit.setEnabled(false);
			
			// Check that device has Internet connection
			
			if (isNetworkAvailable()) {
			
				// Find the deviceid 
				
				SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
				SharedPreferences.Editor editor = settings.edit();

				Integer registeredDevice = Integer.parseInt(settings.getString("registeredDevice", "0"));
				String deviceid = settings.getString("deviceid","");
				
	    		// If no deviceid, create a new one and store it
	    		
	    		
	    		if (deviceid.equalsIgnoreCase("")) {
				
					deviceid = Long.toHexString(Double.doubleToLongBits(Math.random()));

		    		editor.putString("deviceid",deviceid);
		    		editor.commit();
					
				}
				
				// Check that deviceid is registered in database
				
				if (registeredDevice == 0) {
				
					try {
						
						HttpClient httpclient = new DefaultHttpClient();
			     		HttpPost httpPostDeviceExists = new HttpPost("https://www.doc.ic.ac.uk/~gljones/openbattery/device-exists.php");
			     		
			     		List<NameValuePair> nameValuePairsDeviceExists = new ArrayList<NameValuePair>(2);
			     		nameValuePairsDeviceExists.add(new BasicNameValuePair("fromdevice", "1"));
			     		nameValuePairsDeviceExists.add(new BasicNameValuePair("obversion", "1.0"));
			     		nameValuePairsDeviceExists.add(new BasicNameValuePair("deviceid", deviceid));
						httpPostDeviceExists.setEntity(new UrlEncodedFormEntity(nameValuePairsDeviceExists));
					
						HttpResponse responseDeviceExists = httpclient.execute(httpPostDeviceExists);

						String responseStr = URLEncoder.encode(org.apache.http.util.EntityUtils.toString(responseDeviceExists.getEntity()));

						Log.d("openbattery","deviceexists? " + responseStr);
						
						// If device does not exist, create device

						if (responseStr.equalsIgnoreCase("notexists")) {
				        	
				    		HttpPost httpPostDeviceCreate = new HttpPost("https://www.doc.ic.ac.uk/~gljones/openbattery/device-create.php");
				     		
				     		List<NameValuePair> nameValuePairsDeviceCreate = new ArrayList<NameValuePair>(2);
				     		nameValuePairsDeviceCreate.add(new BasicNameValuePair("fromdevice", "1"));
				     		nameValuePairsDeviceCreate.add(new BasicNameValuePair("obversion", "1.0"));
				     		nameValuePairsDeviceCreate.add(new BasicNameValuePair("deviceid", deviceid));
				     		nameValuePairsDeviceCreate.add(new BasicNameValuePair("devicemodel", android.os.Build.MODEL));
				     		nameValuePairsDeviceCreate.add(new BasicNameValuePair("manufacturer", android.os.Build.MANUFACTURER));
				     		nameValuePairsDeviceCreate.add(new BasicNameValuePair("model", android.os.Build.MODEL));
				     		nameValuePairsDeviceCreate.add(new BasicNameValuePair("technology", android.os.BatteryManager.EXTRA_TECHNOLOGY));
				     		
				     		Integer scale=0;
				     		try {
				     			scale = Integer.parseInt(android.os.BatteryManager.EXTRA_SCALE);
				     		} catch (NumberFormatException e) {
				     			scale = 0;
				     		}
				     		
				     		nameValuePairsDeviceCreate.add(new BasicNameValuePair("designcapacity", scale.toString()));
				     		httpPostDeviceCreate.setEntity(new UrlEncodedFormEntity(nameValuePairsDeviceCreate));
							
			     			HttpResponse responseDeviceCreate = httpclient.execute(httpPostDeviceCreate);

					        String responseStrDeviceCreate = URLEncoder.encode(org.apache.http.util.EntityUtils.toString(responseDeviceCreate.getEntity())).trim();

					        Log.d("openbattery","deviceadded? "+ responseStrDeviceCreate);
					        Log.d("devicescale",scale.toString());
					        
					        if (responseStrDeviceCreate.equalsIgnoreCase("deviceregistrationsuccessful")){
					        
					        	// New device linked to this user's account
					        	
					        	Toast.makeText(getApplicationContext(), "New device registered", Toast.LENGTH_SHORT);
					    		editor.putString("deviceRegistered","1");
					    		editor.commit();
					        	
					        }

						}
			        
					} catch (Exception e) {
						// TODO
					}
					
				}
				
				
				// Now submit data
				
				//OpenBatteryService.submitData(deviceid);				
				
				Log.d("openbattery","Manual submit clicked");
				
				new submitDataTask().execute();
				
			} else {
			
				Toast.makeText(getApplicationContext(), "Open Battery requires an Internet connection to submit data", Toast.LENGTH_SHORT).show();
				
			}

			btSubmit.setEnabled(true);			

		}
		
	};

	// Create menu
	
	public boolean onCreateOptionsMenu(Menu menu) {
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.main_menu, menu);
	    return true;
	}
	
	// Deal with clicks on menu items
	
	public boolean onOptionsItemSelected(MenuItem item) {

		SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
		
		String deviceid = settings.getString("deviceid","");
    	
		// Handle item selection
	    switch (item.getItemId()) {
	    case R.id.viewlogfile:
	    	Intent intent = new Intent();
            intent.setAction(Intent.ACTION_VIEW);
            intent.setDataAndType(Uri.parse("file://" + "/sdcard/" + "OpenBattery/battery.csv"), "text/*");
            startActivity(intent);
	        return true;
	    case R.id.viewdeviceid:
	    	
    		AlertDialog.Builder builder = new AlertDialog.Builder(this);
    		builder.setMessage("Your device ID is " + deviceid);
    		builder.setNeutralButton("OK", new DialogInterface.OnClickListener() {
    			   public void onClick(DialogInterface dialog, int which) {
    			      // here you can add functions
    			   }
    			});
    		builder.show();
    		//AlertDialog alert = builder.create();
    		
	        return true;
	    case R.id.viewstats:
	    	
	    	// Link to online web page showing statistics

	    	// From http://stackoverflow.com/questions/2201917/
	    	
	    	String urlStats = "http://www.doc.ic.ac.uk/~gljones/openbattery/device-graph.php?deviceid=" + deviceid;   	
	    	
	    	Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(urlStats));
	    	startActivity(browserIntent);
	    	
	    	return true;
	    default:
	        return super.onOptionsItemSelected(item);
	    }
	}
	
	OnCheckedChangeListener updateAutoSendPrefs = new OnCheckedChangeListener() {
		
		public void onCheckedChanged(RadioGroup radioGroupAutoSend, int checkedId) {

	    	Log.d("openbattery","in updateAutoSendPrefs onCheckedChanged");
	    	
	    	SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
			
	        //if (((CheckBox) v).isChecked()) {
	            
	    	int selectedId = radioGroupAutoSend.getCheckedRadioButtonId();
	    	
	    	if (selectedId == radioButtonCharging.getId()) {
	    		
	    		SharedPreferences.Editor editor = settings.edit();
	    		editor.putString("autoSubmit","1");
	    		editor.commit();


	    	} else if (selectedId == radioButtonChargingWifi.getId()) {
	    		
	    		SharedPreferences.Editor editor = settings.edit();
	    		editor.putString("autoSubmit","2");
	    		editor.commit();
	    		
	    	} else {
	    		
	    		SharedPreferences.Editor editor = settings.edit();
	    		editor.putString("autoSubmit","0");
	    		editor.commit();
	    		
	    	}
			
	    }

	};
	
	
	class submitDataTask extends AsyncTask<Void, Integer, String> {
		
		@Override
		protected String doInBackground(Void... params) {
			
			SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
			SharedPreferences.Editor editor = settings.edit();
			
			//
			// GET DEVICE ID
			//
			
    		String deviceid = settings.getString("deviceid","");

    		if (deviceid.equalsIgnoreCase("")) {
				
				deviceid = Long.toHexString(Double.doubleToLongBits(Math.random()));

	    		editor.putString("deviceid",deviceid);
	    		editor.commit();
				
			}
    		
    		//
    		// REGISTER DEVICE
    		//
    		
    		try {
    			
    			HttpClient httpclient = new DefaultHttpClient();
         		HttpPost httpPostDeviceExists = new HttpPost("https://www.doc.ic.ac.uk/~gljones/openbattery/device-exists.php");
         		
         		List<NameValuePair> nameValuePairsDeviceExists = new ArrayList<NameValuePair>(2);
         		nameValuePairsDeviceExists.add(new BasicNameValuePair("fromdevice", "1"));
         		nameValuePairsDeviceExists.add(new BasicNameValuePair("obversion", "1.0"));
         		nameValuePairsDeviceExists.add(new BasicNameValuePair("deviceid", deviceid));
    			httpPostDeviceExists.setEntity(new UrlEncodedFormEntity(nameValuePairsDeviceExists));
    		
    			HttpResponse responseDeviceExists = httpclient.execute(httpPostDeviceExists);

    			String responseStr = URLEncoder.encode(org.apache.http.util.EntityUtils.toString(responseDeviceExists.getEntity()));

    			//Log.d("openbattery","deviceexists? " + responseStr);
    			
    			// If device does not exist, create device

    			if (responseStr.equalsIgnoreCase("notexists")) {
    	        	
    	    		HttpPost httpPostDeviceCreate = new HttpPost("https://www.doc.ic.ac.uk/~gljones/openbattery/device-create.php");
    	     		
    	     		List<NameValuePair> nameValuePairsDeviceCreate = new ArrayList<NameValuePair>(2);
    	     		nameValuePairsDeviceCreate.add(new BasicNameValuePair("fromdevice", "1"));
    	     		nameValuePairsDeviceCreate.add(new BasicNameValuePair("obversion", "1.0"));
    	     		nameValuePairsDeviceCreate.add(new BasicNameValuePair("deviceid", deviceid));
    	     		nameValuePairsDeviceCreate.add(new BasicNameValuePair("devicemodel", android.os.Build.MODEL));
    	     		nameValuePairsDeviceCreate.add(new BasicNameValuePair("manufacturer", android.os.Build.MANUFACTURER));
    	     		nameValuePairsDeviceCreate.add(new BasicNameValuePair("model", android.os.Build.MODEL));
    	     		nameValuePairsDeviceCreate.add(new BasicNameValuePair("technology", android.os.BatteryManager.EXTRA_TECHNOLOGY));
    	     		
    	     		Integer scale=0;
    	     		try {
    	     			scale = Integer.parseInt(android.os.BatteryManager.EXTRA_SCALE);
    	     		} catch (NumberFormatException e) {
    	     			scale = 0;
    	     		}
    	     		
    	     		nameValuePairsDeviceCreate.add(new BasicNameValuePair("designcapacity", scale.toString()));
    	     		httpPostDeviceCreate.setEntity(new UrlEncodedFormEntity(nameValuePairsDeviceCreate));
    				
         			HttpResponse responseDeviceCreate = httpclient.execute(httpPostDeviceCreate);

    		        String responseStrDeviceCreate = URLEncoder.encode(org.apache.http.util.EntityUtils.toString(responseDeviceCreate.getEntity())).trim();

    		        //Log.d("openbattery","deviceadded? "+ responseStrDeviceCreate);
    		        
    		        if (responseStrDeviceCreate.equalsIgnoreCase("deviceregistrationsuccessful")){
    		        
    		        	// New device linked to this user's account
    		        	
    		        	Toast.makeText(getApplicationContext(), "New device registered", Toast.LENGTH_SHORT);
    		    		editor.putString("deviceRegistered","1");
    		    		editor.commit();
    		        	
    		        }

    			}
            
    		} catch (Exception e) {
    			// TODO
    		}
    		
    		
    		//
    		// SEND DATA
    		//
    		
    		String url = "https://www.doc.ic.ac.uk/~gljones/openbattery/device-insert-data-file.php";
    		File file = new File(Environment.getExternalStorageDirectory(),
    				OpenBatteryService.LOGFILEPATH);

    		//Log.d("openbattery","log file canwrite " + file.canWrite());
    		//Log.d("openbattery","log file is " + file.toString());

			Long fileLength = file.length();
			Long readLength = (long) 0;
    		
    		if (fileLength < 10) {
    			
    			return "blank";
    			
    		}
    		
    		try {
    			HttpClient httpclient = new DefaultHttpClient();

    			HttpPost httppost = new HttpPost(url);

    			// Read the file

    			BufferedReader br = new BufferedReader(new FileReader(file));

    			String line;
    			String fileContents = "";
    			int i = 1;

    			boolean keepSubmitting = true;
    			List<NameValuePair> nameValuePairsSubmitFile = new ArrayList<NameValuePair>(
    					2);

    			while (((line = br.readLine()) != null) && (keepSubmitting == true)) {

    				readLength = readLength + line.length();
    				publishProgress((int) ((readLength*100)/fileLength));
    				
    				String amendedLine = line.replace(';',' ');
    				fileContents = fileContents.concat(amendedLine);
    				fileContents = fileContents.concat("\n");

    				i++;

    				if (i % 250 == 0) {

    					// Every 100000 lines send the data (if successful) continue
    					// and reset the data;

    					nameValuePairsSubmitFile.clear();
    					nameValuePairsSubmitFile.add(new BasicNameValuePair(
    							"fromdevice", "1"));
    					nameValuePairsSubmitFile.add(new BasicNameValuePair(
    							"obversion", "2.0"));
    					
    					nameValuePairsSubmitFile.add(new BasicNameValuePair("deviceid",
    							deviceid));
    					
    					nameValuePairsSubmitFile.add(new BasicNameValuePair(
    							"data-file", fileContents));
    					httppost.setEntity(new UrlEncodedFormEntity(
    							nameValuePairsSubmitFile));

    					HttpResponse response = httpclient.execute(httppost);

    					String responseStr = URLEncoder.encode(
    							org.apache.http.util.EntityUtils.toString(response
    									.getEntity())).trim();

    					//Log.d("openbattery", "response " + responseStr);

    					if (!responseStr.equalsIgnoreCase("insertfilesuccessful")) {

    						// stop submitting if not successful
    						keepSubmitting = false;

    					}

    					fileContents = "";

    				} // end if
    			} // end while

    			// Submit remaining data, clear data file if successful

    			nameValuePairsSubmitFile.clear();
    			nameValuePairsSubmitFile.add(new BasicNameValuePair("fromdevice",
    					"1"));
    			nameValuePairsSubmitFile.add(new BasicNameValuePair("obversion",
    					"2.0"));
    			
    			nameValuePairsSubmitFile.add(new BasicNameValuePair("deviceid",
    					deviceid));
    			nameValuePairsSubmitFile.add(new BasicNameValuePair("data-file",
    					fileContents));
    			httppost.setEntity(new UrlEncodedFormEntity(
    					nameValuePairsSubmitFile));

    			HttpResponse response = httpclient.execute(httppost);

    			String responseStr = URLEncoder.encode(
    					org.apache.http.util.EntityUtils.toString(response
    							.getEntity())).trim();

    			// Log.d("openbattery", "response " + responseStr);

    			br.close();
    			
    			if (responseStr.equalsIgnoreCase("insertfilesuccessful") && keepSubmitting) {

    				// on successful insert, delete file
    				return "insertfilesuccessful";

    			} else {

    				if (responseStr.equalsIgnoreCase("blank")) {

    					// on successful insert, delete file
    					Log.e("openbattery", "blank file, no data to submit");

    					return "blank";
    					
    				} else {
    				
    				// not successful
    				Log.e("openbattery", "recs not inserted successfuly");
    				return "error";
    				}
    			}

    		} catch (Exception e) {
    			// show error
    			return "error";
    		}
    		
		}	
    		

		protected void onPreExecute() {
			mProgress.setProgress(0);
			mProgress.setVisibility(View.VISIBLE);
		}
		
		protected void onProgressUpdate(Integer... progress) {
	        //setProgressPercent(progress);
			mProgress.setProgress(progress[0]);
		}

		protected void onPostExecute(String result) {
			
			mProgress.setVisibility(View.INVISIBLE);
			
			//Log.d("openbattery","result is "+ result);
			
			if (result.equalsIgnoreCase("insertfilesuccessful")) {
				Toast.makeText(getApplicationContext(), "Successfully submitted data", Toast.LENGTH_SHORT).show();
				
	    		File file = new File(Environment.getExternalStorageDirectory(),
	    				OpenBatteryService.LOGFILEPATH);
				
				// delete file
	    		file.delete();
    		
	    		try {
	    			file.createNewFile();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
			if (result.equalsIgnoreCase("blank")) {
				Toast.makeText(getApplicationContext(), "No data to submit", Toast.LENGTH_SHORT).show();
			}
			if (result.equalsIgnoreCase("error")) {
				Toast.makeText(getApplicationContext(), "Error in submitting data", Toast.LENGTH_SHORT).show();
			}
		}

	 }
	
}